package com.example.controller.contractManage;

import com.alibaba.excel.EasyExcel;
import com.example.entity.Contract;
import com.example.entity.ContractVO;
import com.example.entity.SysStaff;
import com.example.service.contractManage.ContractSigningService;
import com.example.util.R;
import com.example.util.excelListener.ContractVOListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ----MaxCV工程师---
 *
 * @author wangbiao
 * @date2024/12/3
 * @desc
 */
@RestController
@RequestMapping("/sys/contract")
@Log4j2
public class ContractSigningController {

    @Autowired
    private ContractSigningService contractSigningService;

    /**
     * 查询全部合同
     */
    @PostMapping("/contractList")
    public R contractList(@RequestBody ContractVO contractVO){
        List<ContractVO> list = contractSigningService.contractList(contractVO);
//        System.out.println(list);

        PageInfo<ContractVO> info = new PageInfo<>(list);

        return R.ok(info);

    }


    /**
     * 删除单条合同
     */
    @GetMapping("/del")
    public R del(int contractId){
        int i = contractSigningService.del(contractId);
        if (i>0){
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 批量删除
     */
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam ArrayList<Integer> ids){
        int i = contractSigningService.delBatch(ids);
        if (i>0){
            return R.ok();
        }
        else return R.fail();

    }

    /**
     * 添加时根据输入的人名查找身份证
     */
    @GetMapping("/getCardId")
    public R getCardId(String staffName){
        SysStaff sysStaff = contractSigningService.getCardId(staffName);
        if (sysStaff != null){
            return R.ok(sysStaff);
        }
        return R.fail("没有这名员工,检查后输入");
    }

    /**
     * 添加合同信息
     */
    @PostMapping("/addContract")
    public R addContract(@RequestBody ContractVO contractVO){
        int i = contractSigningService.addContract(contractVO);
        if (i>0){
            return R.ok();
        }
       return R.fail("没有插入成功");
    }

    /**
     * 更改合同信息
     */
    @PostMapping("/update")
    public R update(@RequestBody ContractVO contractVO){

        int i = contractSigningService.update(contractVO);
        if (i>0){
            return R.ok();
        }
        return R.fail("更新失败");
    }

    /**
     * 提交按钮,点击提交,合同表的check_state状态改为0
     */
    @GetMapping("/submit")
    public R submit(int contractId){
        int i = contractSigningService.submit(contractId);
        if (i>0){
            return R.ok();
        }
        return R.fail("提交失败");
    }

    /**
     * 撤销按钮,点击撤销,合同表的check_state状态改为3
     */
    @GetMapping("/callBack")
    public R callBack(int contractId){
        int i = contractSigningService.callBack(contractId);
        if (i>0){
            return R.ok();
        }
        return R.fail("撤销失败");
    }


    /**
     * 合同类型报表展现
     */
    @GetMapping("/echars")
    public R contractTypeEChars(){
        // 这个sql语句需要用List<Map>来接收
        // 第一版
     /*   HashMap<String,ArrayList<Object>> map = new HashMap<>();
        ArrayList<Object> x= new ArrayList<>();
        x.add("劳动合同");
        x.add("派遣合同");
        x.add("临时合同");
        map.put("xType",x);

        ArrayList<Object> y= new ArrayList<>();
        y.add(4);
        y.add(10);
        y.add(2);
        map.put("yValue",y);*/

        // 第二版
        HashMap<String,ArrayList<Object>> countMap = new HashMap<>();
        HashMap<String,Object> map = contractSigningService.contractTypeEChars();
        Object oneNum = map.get("劳务合同");
        Object twoNum = map.get("派遣合同");
        Object threeNum = map.get("临时合同");

        ArrayList<Object> xType = new ArrayList<>();
        xType.add("劳动合同");
        xType.add("派遣合同");
        xType.add("临时合同");
        ArrayList<Object> yValue = new ArrayList<>();
        yValue.add(oneNum);
        yValue.add(twoNum);
        yValue.add(threeNum);
        countMap.put("xType",xType);
        countMap.put("yValue",yValue);

        return R.ok(countMap);
    }


    /**
     * 上传excel表格
     */
    @PostMapping("/upload/excel")
    @CrossOrigin
    public R uploadExcel(MultipartFile file)  {
        // 调用工具类方法处理excel表格
        try {
            EasyExcel.read(file.getInputStream(), ContractVO.class, new ContractVOListener(contractSigningService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }


    /**
     * 导出计量单位到excel表格
     */
    @GetMapping("export/excel")
    public void exportExcel(HttpServletResponse response) throws IOException {

        ContractVO contractVO = new ContractVO();
        List<ContractVO> list = contractSigningService.contractList(contractVO);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("合同签订数据","UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ContractVO.class).sheet("合同签订数据").doWrite(list);

    }
}
